$(document).ready(function() {
	
	$("<link rel='stylesheet' type='text/css' href='/img/jquery.ui.autocomplete.css' />").appendTo("head");
	$.ajax({type : "POST",
	  				url : "/indexer/suggest",
	  				dataType: 'json',   
	  				data : {input: 'biologi'}
	 			});
	
	$("#psh_search").live('keyup', function(event){
  		var keycode = (event.keyCode ? event.keyCode : event.which);
  		var textInput = $(this).val();
  		var pshTest = $('li.ui-menu-item').filter(function() {
			return $(this).text() == textInput;
		});
  		if(keycode == '13'){
  			var subjectTest = $('span.test').filter(function() {
				return $(this).text() == textInput;
		   });
   		if(subjectTest.text().length > 0){
   			$('#alreadyMsg').html('<span id="temp">    Požadované heslo již bylo přiřazeno.</span>');
   			$('#temp').fadeOut(3000);
   		}
   		else{
   			if(pshTest.text().length > 0){
  					$.ajax({type : "POST",
	  				url : "/indexer/translate", 
	  				success: function(data){
	  					if(data == "pref"){
  							$("<li class='subject'><span class='subjectValue test' onClick='getDocBySubject(this)'>" +textInput +"</span> <span class='removeSuggested' onClick='remove(this)'>×</span></li>").appendTo("#suggestedSubjects");		  						 
	  					}
	  					else{
  							$("<li class='subject'><span class='subjectValue test' onClick='getDocBySubject(this)'>" + data +"</span> (<span class='alt test'>" + textInput + "</span>) <span class='removeSuggested' onClick='remove(this)'>×</span></li>").appendTo("#suggestedSubjects");		  						 		  					
	  					}
	  					$('#psh_search').val('');
	  					},  
	  				data : {input: textInput}
	 			});
  				}
  			}
  		}
  		else{
 			if(textInput.length > 2){
  				$.ajax({type : "POST",
	  				url : "/indexer/suggest",
	  				dataType: 'json', 
	  				success: function(data){
	  					$("#psh_search").autocomplete({source: data,
	  						minLength: 3,});
	  					},  
	  				data : {input: textInput}
	 			});
			}
		}
	});

});

$("form").keypress(function(e) {
	if (e.which == 13) {
 		return false;
	}
});
	
function nextStep(){
	$("input").filter(function(){
		return $(this).attr("name") == "step";
	}).attr("value", "2");
}

function getDocBySubject(psh){
		var subject = $(psh).text();
		$.ajax({url : '/indexer/searchrec/' + subject, 
				  success : function(data, textStatus, XMLHttpRequest){
				 			  		if (data['recLen'] > 0){
				 			  			$('#docs').html(data['titles']);
				 			  		}
				 			  		else{
				 			  			$('#docs').html("<span class='documentResult'>Nebyly nalezeny žádné dokumenty.</span>");
				 			  		}
				 			  		$('#subject').html(subject);
				 			  		$('#recLen').html(data['recLen']);},
				  dataType : 'json',
				 });
}

function remove(target){
	$(target).parent().remove();
}

function copySubjects(){
	var target = $("#selectedSubjects");
	var subs = $(".subjectValue").each(function(){var sv = $(this);
																 target.append("<option selected='selected'>" + sv.text() + "</option>" );});
	target.css({"width" : "500px"});
}
